let app = getApp()

Page({

  data: {

  },

  onLoad: function (options) {
    let that = this

    wx.getStorage({
      key: 'userInfo',
      success(e){
        app.data.userInfo = e.data
      }
    })

    // getStorage之前没有setStorage会fail
    // if若条件值为null ,为假
    wx.getStorage({
      key: 'Login_ed',
      success(e){
        if(e.data){
          // Login_ed 不为 null，则表示已经登录过了，不用重新获取用户信息
          that.jumpToIndex()
        }
      }
    })
  },
  jumpToIndex: function(){
    wx.switchTab({
      url: '/pages/navigation/index/index'
    });
  },
  getUserProfile: function(){
    let openId = null
    let that = this
    wx.getStorage({
      key: 'openid',
      success(e) {
        openId = e.data
      }
    })
    wx.getUserProfile({
      desc: '获取头像和昵称',
      success(res) {
        let object = {
          id: openId,
          nickname: res.userInfo.nickName,
          avatar: res.userInfo.avatarUrl
        }
        wx.setStorage({
          key: 'userInfo',
          data: object,
        })
        wx.setStorage({
          key: 'Login_ed',
          data: true
        })
        that.jumpToIndex()
      },
      fail(res) {
        console.log("拒绝授权，无法正常登陆")
      }
    });
  },
  btn_login: function() {
    this.getUserProfile()
  },
  btn_check: function () {
    console.log(app.data.userInfo)
  },

  navigator_index: function () {
      this.jumpToIndex()
  }


})